package com.ht.modules.weather.service;

import com.ht.modules.weather.entity.WeatherDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
@Slf4j
public class WeatherService {
    private static final String WEATHER_OPEN_API_URL = "http://t.weather.sojson.com/api/weather/city/";
    @Autowired
    RestTemplate restTemplate;
    public WeatherDto getWeatherByCityCode(String cityCode) {
        try {
            ResponseEntity<WeatherDto> forEntity = restTemplate.getForEntity(WEATHER_OPEN_API_URL + cityCode, WeatherDto.class);
            HttpStatusCode statusCode = forEntity.getStatusCode();
            if (statusCode.value() == 200) {
                return forEntity.getBody();
            }
        } catch (Exception e) {
            log.error("获取{{}}天气异常", cityCode);
        }
        return null;
    }
}
